import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/LoginView.vue'),
        meta: { requiresAuth: false }
    },
    {
        path: '/user/center',
        name: 'userCenter',
        component: () => import('../views/user/CenterView.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/map',
        name:'mapView',
        component: () => import('../views/map/MapView.vue')
    },
    {
        path: '/course',
        name: 'course',
        component: () => import('../views/course/CourseView.vue')
    },    {
        path: '/trade',
        name: 'trade',
        component: () => import('../views/trade/TradeView.vue')
    },    {
        path: '/gym',
        name: 'gym',
        component: () => import('../views/gym/GymView.vue')
    },
    {
        path: '/gym/management',
        name: 'gymManagement',
        component: () => import('../views/gym/GymManagement.vue'),
        meta: { requiresAuth: true }
    },    {
        path: '/chat',
        name: 'chat',
        component: () => import('../views/chat/ChatView.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/reservation/create',
        name: 'reservationCreate',
        component: () => import('../views/reservation/ReservationCreateView.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/reservation/my',
        name: 'myReservations',
        component: () => import('../views/reservation/MyReservationsView.vue'),
        meta: { requiresAuth: true }
    },
    {
        path: '/reservation/manage',
        name: 'reservationManage',
        component: () => import('../views/reservation/ReservationManageView.vue'),
        meta: { requiresAuth: true }
    },
    // 修改推广活动路由配置
    {
      path: '/activity/promotion',
      name: 'Promotion',
      component: () => import('../views/activity/Promotion.vue')
    }
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
})

// 路由守卫 - 简化版本，只检查用户登录状态
router.beforeEach((to, from, next) => {
    console.log('路由守卫：准备跳转到', to.path)
    
    // 如果访问登录页，直接放行
    if (to.path === '/login') {
        next()
        return
    }
    
    // 简单检查localStorage中是否有用户信息
    const user = localStorage.getItem('user')
    if (!user) {
        console.log('路由守卫：未找到用户信息，跳转到登录页')
        next('/login')
        return
    }
    
    // 有用户信息就放行，不做复杂的token验证
    console.log('路由守卫：找到用户信息，允许访问')
    next()
})

export default router